<?php
class File_load {
	private $filetypes;//数组
	private $filesize;
	private $loadpath;
        private $newnames='';
	private $getmessage='';
        private $load_lable_name;
	function __construct($load_lable_name,$filetypes='',$filesize='',$loadpath='load_dir'){
	
		$this->filetypes=$filetypes;
		$this->filesize=$filesize;
		$this->loadpath=$loadpath;
		$this->load_lable_name=$load_lable_name;
	
	}
	function upload(){
		//var_dump($_FILES[$this->load_lable_name]['type']);
		if(is_array($_FILES[$this->load_lable_name]['name'])){
			for($i=0;$i<count($_FILES[$this->load_lable_name]['name']);$i++){
				if ($_FILES[$this->load_lable_name]['size'][$i]>$this->filesize||$_FILES[$this->load_lable_name]['error'][$i]==1||$_FILES[$this->load_lable_name]['error'][$i]==2)			
				{
					$this->getmessage=$_FILES[$this->load_lable_name]['name'][$i].'文件大小超出限制';
					return false;
				} else if ($_FILES[$this->load_lable_name]['error'][$i]==3){
					$this->getmessage=$_FILES[$this->load_lable_name]['name'][$i].'文件被部分上传';
					return false;	
				} else if ($_FILES[$this->load_lable_name]['error'][$i]==4){
					continue;
				} else if (!in_array($_FILES[$this->load_lable_name]['type'][$i],$this->filetypes)){
					echo $_FILES[$this->load_lable_name]['type'][$i];
					$this->getmessage=$_FILES[$this->load_lable_name]['name'][$i].'文件类型不匹配';
					return false;
				}

			}
			if(implode('',$_FILES[$this->load_lable_name]['name'])==''){
				$this->getmessage='没有上传任何文件';
				return false;

			}
			for($i=0;$i<count($_FILES[$this->load_lable_name]['name']);$i++){
				if (is_uploaded_file($_FILES[$this->load_lable_name]['tmp_name'][$i])){
						$str=substr($_FILES[$this->load_lable_name]['name'][$i],strrpos($_FILES[$this->load_lable_name]['name'][$i],'.'));
						//$str=ltrim($str,'/');
						if(!file_exists($this->loadpath)){
							mkdir($this->loadpath);
						}
						$newname='up_'.date('YmdHis',time()).rand(0,100).$str;
						move_uploaded_file($_FILES[$this->load_lable_name]['tmp_name'][$i],$this->loadpath.'/'.$newname);
                                                $this->newnames.=$newname.'@';
						
				}
			}
                        $this->newnames=rtrim($this->newnames,'@');
			return true;

		} else {
			if ($_FILES[$this->load_lable_name]['size']>$this->filesize||$_FILES[$this->load_lable_name]['error']==1||$_FILES[$this->load_lable_name]['error']==2){
				$this->getmessage='文件大小超出限制';
				return false;
			} else if ($_FILES[$this->load_lable_name]['error']==3){
				$this->getmessage='文件被部分上传';
				return false;	
			} else if ($_FILES[$this->load_lable_name]['error']==4){
				$this->getmessage='没有文件被上传';
				return false;	
			} else if (!in_array($_FILES[$this->load_lable_name]['type'],$this->filetypes)){
				$this->getmessage='文件类型不匹配';
				return false;
			} else {
				if (is_uploaded_file($_FILES[$this->load_lable_name]['tmp_name'])){
					$str=substr($_FILES[$this->load_lable_name]['name'],strrpos($_FILES[$this->load_lable_name]['name'],'.'));
					//$str=ltrim($str,'/');
					if(!file_exists($this->loadpath)){
						mkdir($this->loadpath);
					}
					$this->newnames='up_'.date('YmdHis',time()).rand(0,100).$str;
					move_uploaded_file($_FILES[$this->load_lable_name]['tmp_name'],$this->loadpath.'/'.$this->newnames);
					return true;
				}
			}
		}

	}
	function __get($proname){
		return $this->$proname;
		
	}
}
